using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Configuration;
using System.IO;
using System.Xml.Serialization;
using System.Collections;
using System.Windows.Forms;
using LeisureCenter.Api;
using LeisureCenter.Tools;

namespace LeisureCenter.Core
{
    public class Extension
    {
        private string player = "";

        private MediaKind kind = MediaKind.Unknown;

        [XmlAttribute("kind")]
        public MediaKind Kind
        {
            get { return kind; }
            set { kind = value; }
        }

        [XmlAttribute("player")]
        public string Player { get { return player; } set { player = value; } }
    }

    public class NamedExtension : Extension
    {
        private string name;

        [XmlAttribute("name")]
        public string Name
        {
            get { return name; }
            set
            {
                name = value;
                if (!string.IsNullOrEmpty(name) && !name.StartsWith("."))
                {
                    name = "." + name;
                }
            }
        }
    }

    public class FileSystemBookmark
    {
        [XmlAttribute("name")]
        public string Name = "";

        [XmlAttribute("path")]
        public string Path = "";
    }

    public class ExternalApplication
    {
        [XmlAttribute("name")]
        public string Name = "";

        [XmlAttribute("path")]
        public string Path = "";

        [XmlAttribute("iconPath")]
        public string IconPath = "";
    }

    public class WebUI
    {
        #region private members

        private bool _enabled = true;
        private ushort _port = 5222;

        #endregion

        #region public members

        public bool Enabled
        {
            get { return _enabled; }
            set { _enabled = value; }
        }

        public ushort Port
        {
            get { return _port; }
            set { _port = value; }
        }

        #endregion
    }

    public class Settings
    {
        private static string configFilename = LeisureCenterInstance.DataPath + @"\LeisureCenter.config";

        private Dictionary<string, Extension> extensions = new Dictionary<string, Extension>(StringComparer.InvariantCultureIgnoreCase);

        [XmlIgnore()]
        public Dictionary<string, Extension> Extensions { get { return extensions; } }

        [XmlArrayItem("Extension", typeof(NamedExtension))]
        [XmlArray("Extensions")]
        public NamedExtension[] NamedExtensions
        {
            get
            {
                NamedExtension[] result = new NamedExtension[extensions.Count];
                int i = 0;
                foreach (KeyValuePair<string, Extension> ext in extensions)
                {
                    NamedExtension n = new NamedExtension();
                    n.Name = ext.Key;
                    n.Kind = ext.Value.Kind;
                    n.Player = ext.Value.Player;
                    result[i] = n;
                    i++;
                }
                return result;
            }

            set
            {
                foreach (NamedExtension ext in value)
                {
                    extensions.Add(ext.Name, ext);
                }
            }
        }

        public Padding Padding = new Padding(0);

        public bool UpdateToTestVersions;

        private List<FileSystemBookmark> fileSystemBookmarks = new List<FileSystemBookmark>();

        [XmlArrayItem("Bookmark")]
        public List<FileSystemBookmark> FileSystemBookmarks
        {
            get { return fileSystemBookmarks; }
            set { fileSystemBookmarks = value; }
        }

        private List<ExternalApplication> externalApplications = new List<ExternalApplication>();

        [XmlArrayItem("ExternalApplication")]
        public List<ExternalApplication> ExternalApplications
        {
            get { return externalApplications; }
            set { externalApplications = value; }
        }

        private WebUI _webUI = new WebUI();

        public WebUI WebUI
        {
            get { return _webUI; }
            set { _webUI = value; }
        }

        public static Settings LoadSettings()
        {
            if (!File.Exists(configFilename))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(configFilename));
                StreamWriter writer = new StreamWriter(configFilename);
                try
                {
                    writer.Write(Properties.Resources.DefaultSettings);
                }
                finally
                {
                    writer.Close();
                }
            }

            if (File.Exists(configFilename))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(Settings));
                try
                {
                    FileStream stream = new FileStream(configFilename, FileMode.Open);
                    try
                    {
                        return (Settings)serializer.Deserialize(stream);
                    }
                    finally
                    {
                        stream.Close();
                    }
                }
                catch
                {
                    //return new Settings();
                    throw;
                }
            }
            return new Settings();
        }

        public void SaveSettings()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(Settings));
            FileStream stream = new FileStream(configFilename, FileMode.Create);
            try 
            {	        
                serializer.Serialize(stream, this);
            }
            finally
            {
                stream.Close();
    	    }
        }
    }
}
